package reflection;

import java.lang.reflect.Method;
import java.util.Date;

import fileIO.Student;

public class GetPropertyExercise {
	public static void main(String[] args) {
		getter();
	}

	private static void getter() {
		Student stu = new Student("张三","男",23);
		System.out.println(getProperty(stu, "name"));
		System.out.println(getProperty(stu, "age"));
		System.out.println(getProperty(stu, "gender"));
		
		Date d = new Date();
		System.out.println(getProperty(d, "time"));
	}

	private static Object getProperty(Object o, String s) {
		Class c = o.getClass();
		String methodName = "get"+s.substring(0,1).toUpperCase()+s.substring(1);
		Method m = null;
		Object returnVal=null;
		try {
			m=c.getMethod(methodName, null);
			returnVal = m.invoke(o, null);
		} catch (Exception e) {
		}
		return returnVal;
	}
}
